package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.dao.TeacherDao;

public class TeacherService {
    private TeacherDao teacherDao = new TeacherDao();

    public boolean addStudent(Teacher stu) {
        // 2. 将学生对象, 传递给StudentDao 库管中的addStudent方法
        // 3. 将返回的boolean类型结果, 返还给StudentController
        return teacherDao.addStudent(stu);
    }

    // 查看学生方法
    public Teacher[] findAllStudent() {
        // 1. 调用库管对象的findAllStudent获取学生对象数组
        Teacher[] allStudent = teacherDao.findAllStudent();
        // 2. 判断数组中是否有学生信息 (有: 返回地址,  没有: 返回null)
        // 思路: 数组中只要存在一个不是null的元素, 那就代表有学生信息
        boolean flag = false;
        for (int i = 0; i < allStudent.length; i++) {
            Teacher stu = allStudent[i];
            if(stu != null){
                flag = true;
                break;
            }
        }

        if(flag){
            // 有信息
            return allStudent;
        }else{
            // 没有信息
            return null;
        }

    }

    public void deleteStudentById(String delId) {
        teacherDao.deleteStudentById(delId);
    }

    public void updateStudent(String updateId, Teacher newStu) {
        teacherDao.updateStudent(updateId, newStu);
    }

    public boolean isExists(String id) {
        //从StudentDao中获得存储对象的数组，调用内部的findAllStudent方法，该方法返回学生数组
        //StudentDao studentDao = new StudentDao();
        Teacher[] stus=teacherDao.findAllStudent();
        //遍历数组获得每一个学生对象，判断学号是否存在
        boolean exists=false;
        for (int i = 0; i < stus.length; i++) {
            Teacher student=stus[i];
            if(student!=null&&student.getId().equals(id)){
                exists=true;
                break;
            }
        }
        //返回true或false结果
        return exists;
    }
}
